<?php

namespace app\api\module;

use app\common\model\Commission as Commodel;
use app\common\model\UserDistributorOrder;
use app\common\model\Users;
use app\common\model\UserWithdraw;
use think\Db;

class Commission extends Common
{
    /**
     * 判断是否需要登录
     *
     * @param string $interface 待检测的接口
     * @return bool
     */
    protected function needLogin($interface)
    {
        //不需登录的接口
        $ignoreList = [

        ];

        if (in_array($interface, $ignoreList)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 执行接口操作
     *
     * @param $interface
     * @return array|string
     */
    public function exec($interface)
    {
        switch ($interface) {
            //【1001】邀请好友
            case ZFN_COMMISSION_INVITE:
                return $this->invite();
            //【1002】团队人数
            case ZFN_COMMISSION_TEAM:
                return $this->team();
            //【1003】团队列表
            case ZFN_COMMISSION_TEAM_LIST:
                return $this->teamList();
            //【1004】我的钱包
            case ZFN_COMMISSION_WALLET:
                return $this->wallet();
            //【1005】余额记录
            case ZFN_COMMISSION_MONEY_LOG:
                return $this->moneyLog();
            //【1006】积分记录
            case ZFN_COMMISSION_COIN_LOG:
                return $this->coinLog();
            //【1007】提现信息
            case ZFN_COMMISSION_WITHDRAW_INFO:
                return $this->withdrawInfo();
            //【1008】提现提交
            case ZFN_COMMISSION_WITHDRAW_SUB:
                return $this->withdrawSub();
            //【1009】新我的团队数据
            case ZFN_COMMISSION_TEAM_TOP:
                return $this->myTeamTop();
            //【1010】我的团队收益明细
            case ZFN_COMMISSION_TEAM_INCOME:
                return $this->teamIncome();
            //【1011】分销商申请信息
            case ZFN_COMMISSION_DIS_APPLY_INFO:
                return $this->disApplyInfo();
            //【1012】分销商申请提交
            case ZFN_COMMISSION_DIS_APPLY_SUB:
                return $this->disApplySub();
            //【1013】申请分销商支付
            case ZFN_COMMISSION_DIS_APPLY_PAY:
                return $this->disApplyPay();
            //【1014】分销商顶部数据
            case ZFN_COMMISSION_DIS_TOP:
                return $this->disTop();
            //【1015】分销商佣金记录
            case ZFN_COMMISSION_DIS_INCOME:
                return $this->disIncome();
            default:
                send_developer_error("模块【分销】中不存在接口【" . $interface . "】,核实文档后重试!");
        }
    }

    private function invite()
    {
        $user = $this->currentUser();
        $res = Commodel::inviteShare($user);
        $this->insertResponse('', $res);
        return $this->response('邀请好友');
    }

    private function team()
    {
        $user = $this->currentUser();
        $res = Commodel::team($user);
        $this->insertResponse('', $res);
        return $this->response('我的团队人数');
    }

    private function teamList()
    {
        $user = $this->currentUser();
        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $res = Commodel::teamList($user,$page,$page_size);
        $this->insertResponse('data', $res);
        return $this->response('我的团队列表');
    }

    private function wallet()
    {
        $user = $this->currentUser();
        $res = Commodel::wallet($user);
        $this->insertResponse('', $res);
        return $this->response('我的钱包');
    }

    private function moneyLog()
    {
        $user = $this->currentUser();
        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $res = Commodel::moneyList($user,$page,$page_size);

        $this->insertResponse('data', $res);
        return $this->response('余额明细');
    }

    private function coinLog()
    {
        $user = $this->currentUser();
        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $res = Commodel::coinList($user,$page,$page_size);
        $this->insertResponse('data', $res);
        return $this->response('积分明细');
    }

    private function withdrawInfo()
    {
        $user = $this->currentUser();
        $res = UserWithdraw::info($user);
        $this->insertResponse('', $res);
        return $this->response('提现信息');
    }

    private function withdrawSub()
    {
        $user = $this->currentUser();
        $money = $this->essentialParam('money');
        $type = $this->essentialParam('type');
        $name = $this->essentialParam('name');
        $account = $this->essentialParam('account');
        $bank_name = $this->essentialParam('bank_name');
        $bank_branch = $this->optionalParam('bank_branch');
        $res = UserWithdraw::sub($user, $money, $type, $name,$account,$bank_name,$bank_branch);
        $this->insertResponse('', $res);
        return $this->response('提现申请已提交，请等待审核');
    }

    private function myTeamTop()
    {
        $user = $this->currentUser();
        $data = Commodel::myTeamTop($user);
        $this->insertResponse('data', $data);
        return $this->response('新我的团队数据');
    }

    private function teamIncome()
    {
        $user = $this->currentUser();
        $left_time = $this->optionalParam('left_time');
        $right_time = $this->optionalParam('right_time');
        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $data = Commodel::teamIncomeLog($user,$left_time,$right_time,$page,$page_size);
        $this->insertResponse('data', $data);
        return $this->response('我的团队收益明细');
    }

    private function disApplyInfo()
    {
        $user = $this->currentUser();
        $data = UserDistributorOrder::applyInfo($user);
        $this->insertResponse('', $data);
        return $this->response('申请信息');
    }

    private function disApplySub()
    {
        $user = $this->currentUser();
        $name = $this->essentialParam('name');
        $mobile = $this->essentialParam('mobile');
        $source = $this->essentialParam('source');
        $data = UserDistributorOrder::applySub($user,$name,$mobile,$source);
        $this->insertResponse('', $data);
        return $this->response('申请提交订单');
    }

    private function disApplyPay()
    {
        $user = $this->currentUser();
        $order_sn = $this->essentialParam('order_sn');
        $pay_type = $this->essentialParam('pay_type');
        $source = $this->essentialParam('source');
        $data = UserDistributorOrder::applyPay($user,$order_sn,$pay_type,$source);
        $this->insertResponse('', $data);
        return $this->response('申请支付');
    }

    private function disTop()
    {
        $user = $this->currentUser();
        $data = Commodel::disTop($user);
        $this->insertResponse('data', $data);
        return $this->response(get_user_distributor_level(3,2).'顶部数据');
    }

    private function disIncome()
    {
        $user = $this->currentUser();
        $left_time = $this->optionalParam('left_time');
        $right_time = $this->optionalParam('right_time');
        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $data = Commodel::disIncomeLog($user,$left_time,$right_time,$page,$page_size);
        $this->insertResponse('data', $data);
        return $this->response(get_user_distributor_level(3,2).'佣金记录');
    }
}